import datetime
import threading
from Alysis import DownLoadTools
import os
import shutil
import configparser

global index
index = 0

cf = configparser.ConfigParser()
cf.read("./conf/deal.conf")
maxDay = cf.get("option", "max_save_day")
maxInt = int(maxDay)


def mkdir(path):
    path = path.strip()
    path = path.rstrip("\\")
    isExists = os.path.exists(path)

    # 判断结果
    if not isExists:
        os.makedirs(path)

        print
        path + ' 创建成功'
        return True
    else:
        # 如果目录存在则不创建，并提示目录已存在
        print
        path + ' 目录已存在'
        return False


def func():
    global index
    pathStr = os.getcwd() + "//alysisFile//" + str(index % maxInt) + "day"
    downLoad = DownLoadTools()
    downLoad.mainMethod(pathStr)
    # timer = threading.Timer(86400, func)
    # timer.start()
    # index = index + 1


for head in range(maxInt):
    path = os.getcwd()
    cf = configparser.ConfigParser()
    cf.read("./conf/deal.conf")
    ftpList: list = cf.options("source_file")
    for key in ftpList:
        cfgStr = cf.get("source_file", key)
        attributes = cfgStr.split(',')
        mkdir(path + "/alysisFile/" + str(head) + "day/" + attributes[4] + "/lc")
        mkdir(path + "/alysisFile/" + str(head) + "day/" + attributes[4] + "/sc")

logPath = os.getcwd() + '/mainLogs/'
if not os.path.exists(logPath):
    os.makedirs(logPath)

func()
#
#
# # 获取现在时间
# now_time = datetime.datetime.now()
# # 获取明天时间
# next_time = now_time + datetime.timedelta(days=+1)
# next_year = next_time.date().year
# next_month = next_time.date().month
# next_day = next_time.date().day
# # 获取明天5点时间
# next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + " 5:00:00",
#                                        "%Y-%m-%d %H:%M:%S")
# # # 获取昨天时间
# # last_time = now_time + datetime.timedelta(days=-1)
#
# # 获取距离明天5点时间，单位为秒
# timer_start_time = (next_time - now_time).total_seconds()
# print(timer_start_time)
# # 54186.75975
#
#
# # 定时器,参数为(多少时间后执行，单位为秒，执行的方法)
# timer = threading.Timer(timer_start_time, func)
# timer.start()
